package com.cyzy.controller;

import com.cyzy.entity.Class;
import com.cyzy.service.ClassService;
import com.cyzy.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@RequestMapping("/api/class")
public class ClassController {

    @Autowired
    private ClassService classService;

    // 1. 分页查询班级列表（原@GetMapping改为@PostMapping）
    @PostMapping("/list")
    public Result<Map<String, Object>> getClassList(@RequestBody Map<String, Object> params) {
        String keyword = (String) params.get("keyword");
        String grade = (String) params.get("grade");
        int pageNum = params.get("pageNum") != null ? (Integer) params.get("pageNum") : 1;
        int pageSize = params.get("pageSize") != null ? (Integer) params.get("pageSize") : 10;

        Map<String, Object> result = classService.getClassList(keyword, grade, pageNum, pageSize);
        return Result.success(result);
    }

    // 2. 根据ID查询班级（原@GetMapping改为@PostMapping）
    @PostMapping("/getById")
    public Result<Class> getClassById(@RequestBody Map<String, String> params) {
        String id = params.get("id");
        Class classEntity = classService.getClassById(id);
        return Result.success(classEntity);
    }

    // 3. 更新班级（原@PutMapping改为@PostMapping）
    @PostMapping("/update")
    public Result<Boolean> updateClass(@RequestBody Class classEntity) {
        boolean success = classService.updateClass(classEntity);
        return Result.success(success);
    }

    // 4. 删除班级（原@DeleteMapping改为@PostMapping）
    @PostMapping("/delete")
    public Result<Boolean> deleteClass(@RequestBody Map<String, String> params) {
        String id = params.get("id");
        boolean success = classService.deleteClass(id);
        return Result.success(success);
    }
}